<template>
  <div class="contont2">
    <div class="left-box clearfix">
      <i class="topL"></i>
      <i class="topR"></i>
      <i class="bottomL"></i>
      <i class="bottomR"></i>
      <div class="tabtwoallcontent">
        <div class="tabtwo-top-box">
          <el-button size="mini" type="primary" @click="addForm">添 加</el-button>
          {{ this.wrjData.height }}
        </div>
        <div class="tabtwo-con-box">
          <el-table :data="tableData" border center height="100%" style="width: 100%">
            <el-table-column label="" header-align="center">
              <el-table-column prop="xjr1" label="操作时间" align="center" min-width="150">
                <template slot-scope="scope">
                  <span>{{scope.row.czTime}}</span>
                </template>
              </el-table-column>
            </el-table-column>
            <el-table-column label="真值设备A测得" header-align="center">
              <el-table-column prop="xjr1" label="斜距(m)" align="center" min-width="150">
                <template slot-scope="scope">
                  <span v-if="scope.row.id">{{scope.row.xjr1}}</span>
                  <el-input v-else v-model="scope.row.xjr1" placeholder="请输入斜距" size="mini" clearable></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="gdj1" label="高低角(°)" align="center" :min-width="isFlag ? '150' : '600'">
                <template slot-scope="scope">
                  <span v-if="scope.row.id">{{scope.row.gdj1 }}</span>
                  <span v-else class="data-box">
                    <el-input class="data-input" v-model="scope.row.gdj1" placeholder="请输入高低角" size="mini" clearable @input="(val) => handleChangeAngAltitude(val, scope.row)"/>
                    <el-input class="data-input" v-model="scope.row.du" placeholder="请输入度" size="mini" clearable  @input="(val) => handleChangedu(val, scope.row)"/> °
                    <el-input class="data-input" v-model="scope.row.fen" placeholder="请输入分" size="mini" clearable  @input="(val) => handleChangefen(val, scope.row)"/> ′
                    <el-input class="data-input" v-model="scope.row.miao" placeholder="请输入秒" size="mini" clearable  @input="(val) => handleChangemiao(val, scope.row)"/> ″
                  </span>
                </template>
              </el-table-column>
              <el-table-column prop="xjq1" label="舷角(°)" align="center" :min-width="isFlag ? '150' : '600'">
                <template slot-scope="scope">
                  <span v-if="scope.row.id">{{scope.row.xjq1}}</span>
                  <span v-else class="data-box">
                    <el-input class="data-input" v-model="scope.row.xjq1" placeholder="请输入舷角" size="mini" clearable @input="(val) => handleChangexjq1(val, scope.row)"></el-input>
                    <el-input class="data-input" v-model="scope.row.duaxj" placeholder="请输入度" size="mini" clearable @input="(val) => handleChangeduaxj(val, scope.row)"/> °
                    <el-input class="data-input" v-model="scope.row.fenaxj" placeholder="请输入分" size="mini" clearable @input="(val) => handleChangefenaxj(val, scope.row)"/> ′
                    <el-input class="data-input" v-model="scope.row.miaoaxj" placeholder="请输入秒" size="mini" clearable @input="(val) => handleChangemiaoaxj(val, scope.row)"/> ″
                  </span>
                </template>
              </el-table-column>
            </el-table-column>
            <el-table-column label="被试设备B的坐标" header-align="center">
              <el-table-column prop="xb" label="Xb(m)(后负前正)" align="center" min-width="150">
                <template slot-scope="scope">
                  <span v-if="scope.row.id">{{scope.row.xb}}</span>
                  <el-input v-else v-model="scope.row.xb" placeholder="请输入Xb" size="mini" clearable></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="yb" label="Yb(m)(左负右正)" align="center" min-width="150">
                <template slot-scope="scope">
                  <span v-if="scope.row.id">{{scope.row.yb}}</span>
                  <el-input v-else v-model="scope.row.yb" placeholder="请输入Yb" size="mini" clearable></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="zb" label="Zb(m)(下负上正)" align="center" min-width="150">
                <template slot-scope="scope">
                  <span v-if="scope.row.id">{{scope.row.zb}}</span>
                  <el-input v-else v-model="scope.row.zb" placeholder="请输入Zb" size="mini" clearable></el-input>
                </template>
              </el-table-column>
            </el-table-column>
            <el-table-column label="被测设备测量值" header-align="center">
              <el-table-column prop="xb" label="斜距(m)" align="center" min-width="150">
                <template slot-scope="scope">
                  <span v-if="scope.row.id">{{scope.row.bcsbXj}}</span>
                  <el-input v-else v-model="scope.row.bcsbXj" placeholder="请输入斜距" size="mini" clearable></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="bcsbGdj" label="高低角(°)" align="center" min-width="600">
                <template slot-scope="scope">
                  <span v-if="scope.row.id">{{scope.row.bcsbGdj}}</span>
                  <span v-else class="data-box">
                    <el-input class="data-input" v-model="scope.row.bcsbGdj" placeholder="请输入高低角" size="mini" clearable @input="(val) => handleChangebcsbGdj(val, scope.row)"></el-input>
                    <el-input class="data-input" v-model="scope.row.du1" placeholder="请输入度" size="mini" clearable @input="(val) => handleChangedu1(val, scope.row)"/> °
                    <el-input class="data-input" v-model="scope.row.fen1" placeholder="请输入分" size="mini" clearable @input="(val) => handleChangefen1(val, scope.row)"/> ′
                    <el-input class="data-input" v-model="scope.row.miao1" placeholder="请输入秒" size="mini" clearable @input="(val) => handleChangemiao1(val, scope.row)"/> ″
                  </span>
                </template>
              </el-table-column>
              <el-table-column prop="bcsbXianJiao" label="舷角(°)" align="center" min-width="600">
                <template slot-scope="scope">
                  <span v-if="scope.row.id">{{scope.row.bcsbXianJiao}}</span>
                  <span v-else class="data-box">
                    <el-input class="data-input" v-model="scope.row.bcsbXianJiao" placeholder="请输入舷角" size="mini" clearable @input="(val) => handleChangebcsbXianJiao(val, scope.row)"></el-input>
                    <el-input class="data-input" v-model="scope.row.du2" placeholder="请输入度" size="mini" clearable @input="(val) => handleChangedu2(val, scope.row)"/> °
                    <el-input class="data-input" v-model="scope.row.fen2" placeholder="请输入分" size="mini" clearable @input="(val) => handleChangefen2(val, scope.row)"/> ′
                    <el-input class="data-input" v-model="scope.row.miao2" placeholder="请输入秒" size="mini" clearable @input="(val) => handleChangemiao2(val, scope.row)"/> ″
                  </span>
                </template>
              </el-table-column>
            </el-table-column>
            <el-table-column label="计算得出被试设备B的真值" header-align="center">
              <el-table-column prop="xjr2" label="斜距R2(m)" align="center" min-width="150"></el-table-column>
              <el-table-column prop="gdj2" label="高低角β2(°)" align="center" min-width="150">
                <template slot-scope="scope">
                  <span v-if="scope.row.gdj2">{{scope.row.gdj2}}<br/>{{ convertToDMS(scope.row.gdj2) }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="xjq2" label="舷角q2(°)" align="center" min-width="150">
                <template slot-scope="scope">
                  <span v-if="scope.row.xjq2">{{scope.row.xjq2}}<br/>{{ convertToDMS(scope.row.xjq2) }}</span>
                </template>
              </el-table-column>
              <!-- <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                  <el-link type="danger" @click="del(scope.$index)">删除</el-link>
                </template>
              </el-table-column> -->
            </el-table-column>
            <el-table-column label="校准值" header-align="center">
              <el-table-column prop="jzzXj" label="斜距(m)" align="center" min-width="150"></el-table-column>
              <el-table-column prop="jzzGdj" label="高低角(°)" align="center" min-width="150">
                <template slot-scope="scope">
                  <span v-if="scope.row.jzzGdj">{{scope.row.jzzGdj}}<br/>{{ convertToDMS(scope.row.jzzGdj) }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="jzzXianJiao" label="舷角(°)" align="center" min-width="150">
                <template slot-scope="scope">
                  <span v-if="scope.row.jzzXianJiao">{{scope.row.jzzXianJiao}}<br/>{{ convertToDMS(scope.row.jzzXianJiao) }}</span>
                </template>
              </el-table-column>
              
              <!-- <el-table-column prop="jzzXj" label="斜距(m)" align="center" min-width="150">
                <template slot-scope="scope">
                  <span v-if="scope.row.id">{{scope.row.jzzXj}}</span>
                  <el-input v-else v-model="scope.row.jzzXj" placeholder="斜距" clearable></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="jzzGdj" label="高低角(°)" align="center" min-width="150">
                <template slot-scope="scope">
                  <span v-if="scope.row.id">{{scope.row.jzzGdj}}</span>
                  <el-input v-else v-model="scope.row.jzzGdj" placeholder="高低角" clearable></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="jzzXianJiao" label="舷角(°)" align="center" min-width="150">
                <template slot-scope="scope">
                  <span v-if="scope.row.id">{{scope.row.jzzXianJiao}}</span>
                  <el-input v-else v-model="scope.row.jzzXianJiao" placeholder="舷角" clearable></el-input>
                </template>
              </el-table-column> -->
            </el-table-column>
            <el-table-column label="" header-align="center" fixed="right">
              <el-table-column label="操作" center>
                <template slot-scope="scope">
                  <el-link type="danger" @click="del(scope.$index)">删除</el-link>
                </template>
              </el-table-column>
            </el-table-column>
          </el-table>
        </div>
        <div class="tabtwo-bot-box">
          <download-excel class="export-excel-wrapper" :data="tableData" :fields="json_fields" :header="title" name="瞄标定物数据信息.xls">
            <el-button size="mini" type="primary">导 出</el-button>
          </download-excel>
          <el-button size="mini" type="primary" @click="getLdmzjz">历 史 数 据</el-button>
          <el-button size="mini" type="primary" @click="clickAnalysis">开 始 计 算</el-button>
          <el-button size="mini" type="primary" @click="save">保 存 数 据</el-button>
        </div>
      </div>
    </div>
  </div>

</template>

<script>
import mixin from './mixin'
export default {
  name: 'index',
  components: {},
  mixins: [mixin],
  data() {
    return {
    }
  },
  computed: {
    convertToDMS() {
      return (val) => {
        if (val >= 0) {
          const { degree, minute, second } = this.convertDecimalToDMS(val)
          return `${degree}°${minute}′${second}″`
        } else {
          const { degree, minute, second } = this.convertDecimalToDMS(-val)
          return `-${degree}°${minute}′${second}″`
        }
      }
    }
  },
  methods: {
    convertDMSToDecimal(degree, minute, second) {
      // 确保输入值是在有效的范围内
      if (degree < -360 || degree > 360 || minute < -60 || minute >= 60 || second < -60 || second >= 60) {
        return 0
      }
      // 计算十进制度
      let decimalDegree = (+degree) + (+minute) / 60 + (+second) / 3600
      // 如果原始度数是负数，则十进制度也应该是负数
      if (degree < 0) {
        decimalDegree = -decimalDegree
      }
      return parseFloat(decimalDegree.toFixed(4))
    },
    convertDecimalToDMS(decimalDegree) {
      // 确保输入值是在有效的经纬度范围内
      if (decimalDegree < -360 || decimalDegree > 360) {
        return {
          degree: 0,
          minute: 0,
          second: 0
        }
      }
      // 计算度
      let degree = Math.floor(decimalDegree)
      // 计算分
      let minuteNotTruncated = Math.abs(decimalDegree - degree) * 60
      let minute = Math.floor(minuteNotTruncated)
      // 计算秒
      let second = Math.round((minuteNotTruncated - minute) * 60)
      // 返回度分秒对象
      return {
        degree: degree,
        minute: minute,
        second: second
      }
    },
    handleChangeAngAltitude(val, row) {
      console.log('handleChangeAngAltitude', val, row)
      const { degree, minute, second } = this.convertDecimalToDMS(val)
      row.du = degree || 0;
      row.fen = minute || 0;
      row.miao = second || 0;
    },
    handleChangexjq1(val, row) {
      console.log('handleChangexjq1', val, row)
      const { degree, minute, second } = this.convertDecimalToDMS(val)
      row.duaxj = degree || 0;
      row.fenaxj = minute || 0;
      row.miaoaxj = second || 0;
    },
    handleChangebcsbGdj(val, row) {
      console.log('handleChangebcsbGdj', val, row)
      const { degree, minute, second } = this.convertDecimalToDMS(val)
      row.du1 = degree || 0;
      row.fen1 = minute || 0;
      row.miao1 = second || 0;
    },
    handleChangebcsbXianJiao(val, row) {
      console.log('handleChangebcsbXianJiao', val, row)
      const { degree, minute, second } = this.convertDecimalToDMS(val)
      row.du2 = degree || 0;
      row.fen2 = minute || 0;
      row.miao2 = second || 0;
    },
    // 度分秒 转为 十进制度
    handleChangedu(val, row) {
      val = val.replace(/[^\d.]/g,'')
      if (val > 360) {
        val = 360
        row.du = 360
      }
      if (val < -360) {
        val = -360
        row.du = -360
      }
      row.gdj1 = this.convertDMSToDecimal(val, row.fen, row.miao);
    },
    handleChangefen(val, row) {
      val = val.replace(/[^\d.]/g,'')
      if (val >= 60) {
        val = 59
        row.fen = 59
      }
      if (val < -60) {
        val = -59
        row.fen = -59
      }
      row.gdj1 = this.convertDMSToDecimal(row.du, val, row.miao);
    },
    handleChangemiao(val, row) {
      val = val.replace(/[^\d.]/g,'')
      if (val >= 60) {
        val = 59
        row.miao = 59
      }
      if (val < -60) {
        val = -59
        row.miao = -59
      }
      row.gdj1 = this.convertDMSToDecimal(row.du, row.fen, val);
    },

    handleChangeduaxj(val, row) {
      val = val.replace(/[^\d.]/g,'')
      if (val > 360) {
        val = 360
        row.duaxj = 360
      }
      if (val < -360) {
        val = -360
        row.duaxj = -360
      }
      row.xjq1 = this.convertDMSToDecimal(val, row.fenaxj, row.miaoaxj);
    },
    handleChangefenaxj(val, row) {
      val = val.replace(/[^\d.]/g,'')
      if (val >= 60) {
        val = 59
        row.fenaxj = 59
      }
      if (val < -60) {
        val = -59
        row.fenaxj = -59
      }
      row.xjq1 = this.convertDMSToDecimal(row.duaxj, val, row.miaoaxj);
    },
    handleChangemiaoaxj(val, row) {
      val = val.replace(/[^\d.]/g,'')
      if (val >= 60) {
        val = 59
        row.miaoaxj = 59
      }
      if (val < -60) {
        val = -59
        row.miaoaxj = -59
      }
      row.xjq1 = this.convertDMSToDecimal(row.duaxj, row.fenaxj, val);
    },

    handleChangedu1(val, row) {
      val = val.replace(/[^\d.]/g,'')
      if (val > 360) {
        val = 360
        row.du1 = 360
      }
      if (val < -360) {
        val = -360
        row.du1 = -360
      }
      row.bcsbGdj = this.convertDMSToDecimal(val, row.fen1, row.miao1);
    },
    handleChangefen1(val, row) {
      val = val.replace(/[^\d.]/g,'')
      if (val >= 60) {
        val = 59
        row.fen1 = 59
      }
      if (val < -60) {
        val = -59
        row.fen1 = -59
      }
      row.bcsbGdj = this.convertDMSToDecimal(row.du1, val, row.miao1);
    },
    handleChangemiao1(val, row) {
      val = val.replace(/[^\d.]/g,'')
      if (val >= 60) {
        val = 59
        row.miao1 = 59
      }
      if (val < -60) {
        val = -59
        row.miao1 = -59
      }
      row.bcsbGdj = this.convertDMSToDecimal(row.du1, row.fen1, val);
    },

    handleChangedu2(val, row) {
      val = val.replace(/[^\d.]/g,'')
      if (val > 360) {
        val = 360
        row.du2 = 360
      }
      if (val < -360) {
        val = -360
        row.du2 = -360
      }
      row.bcsbXianJiao = this.convertDMSToDecimal(val, row.fen2, row.miao2);
    },
    handleChangefen2(val, row) {
      val = val.replace(/[^\d.]/g,'')
      if (val >= 60) {
        val = 59
        row.fen2 = 59
      }
      if (val < -60) {
        val = -59
        row.fen2 = -59
      }
      row.bcsbXianJiao = this.convertDMSToDecimal(row.du2, val, row.miao2);
    },
    handleChangemiao2(val, row) {
      val = val.replace(/[^\d.]/g,'')
      if (val >= 60) {
        val = 59
        row.miao2 = 59
      }
      if (val < -60) {
        val = -59
        row.miao2 = -59
      }
      row.bcsbXianJiao = this.convertDMSToDecimal(row.du2, row.fen2, val);
    }
  }

}
</script>
<style lang="scss">
@import "./mixins.scss";
::v-deep .el-table__header {
  width: 100% !important;
}
::v-deep .el-table__body-wrapper {
  width: 100%;
}
::v-deep .el-table__body {
  width: 100%;
}

::v-deep .el-table__body-wrapper is-scrolling-none {
  height: calc(100% - 79px) !important;
}

::v-deep .el-table__body-wrapper,
::v-deep .el-table__footer-wrapper,
::v-deep .el-table__header-wrapper {
  height: calc(100% - 79px) !important;
  background: #16356c;
}

.el-input__inner {
  color: #fff;
  // color: #32afef;
  border: 1px solid #32afef;
  background-color: #16356c;
}
.export-excel-wrapper {
  display: inline-block;
  margin-right: 10px;
}
.el-table__body tr.hover-row.current-row>td, .el-table__body tr.hover-row.el-table__row--striped.current-row>td, .el-table__body tr.hover-row.el-table__row--striped>td, .el-table__body tr.hover-row>td {
    background-color: #16356c;
}
</style>
